<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title></title>
<style type="text/css">
* {font-family: Verdana; Tahoma; Arial; font-size: 10pt;}
body {background-color:#FFFFC0;}
</style>
<script>

var whiteListTo = {};
var checkboxChecked = 0;

function update(s)
{
  var div = document.getElementById('tableau');
  div.innerHTML = s;
}

function showDummy()
{
  chrome.windows.getCurrent(
  function(win)
  { 
    chrome.tabs.query({'windowId': win.id, 'active': true},
    function(tabArray)
    {
      var blocked = chrome.extension.getBackgroundPage().blocked[tabArray[0].id];
      if(blocked != undefined)
      {
        var domain = chrome.extension.getBackgroundPage().getTabHost(tabArray[0].id);
 
        var s = 'The following content has been blocked in <b>' + domain + '</b><br /><input type="button" id="allow" value="Allow all requests from this host" onClick="submitOrigin(\'' + domain + '\')">';
        s += '<input type="button" id="edit" value="Edit whitelists" onClick="editWhiteLists()">';
        s += '<div id="domains" style="border:1px dotted;padding:5px;width:340px;overflow-x:scroll;"><b>Domains</b>:<br />';
        for(i = 0; i < blocked.domains.length; i++)
        {
          s += '<input type="checkbox" id="' + blocked.domains[i] + '" onChange="checkboxClicked(this)"><label for="' + blocked.domains[i] + '">' + blocked.domains[i] + '</label><br />';
        }
        s += '<input type="checkbox" id="customcheck" onChange="checkboxClicked(this)"><input type="edit" id="custominput" value="(custom value)" onClick="clearStub()" onChange="writeCustomItem()" style="color:gray"><br /><span style="font-size:8pt">Leading . is allowed as subdomain mask (.domain.com)</span><br />';
        s += '<input type="button" id="white" disabled="true" value="Allow requests to selected domains" onClick="submitWhitelist()"><input type="checkbox" id="reload" checked="true" tabid="' + tabArray[0].id + '"><label for="reload">Reload</label></div>';
        s += '<div id="more" onClick="showLinks()" style="cursor:pointer">Click here to view all links</div>';
        s += '<div id="links" style="display:none;border:1px dotted;padding:5px;width:340px;overflow-x:scroll;"><b>Links:</b><br />';
        checkboxChecked = 0;

        for(i = 0; i < blocked.urls.length; i++)
        {
          s += blocked.urls[i] + '<br />';
        }
        
        s += '</div>';
        update(s);
      }
      else
      {
        var s = 'There is no blocked content in this page<br />';
        s += '<input type="button" id="edit" value="Edit whitelists" onClick="editWhiteLists()">';
        update(s);
      }
    }); 
  });
}

function log(s)
{
  chrome.extension.getBackgroundPage().log(s);
}

function showLinks()
{
  var div = document.getElementById('links');
  if(div)
  {
    div.style.display = "block";
  }
  div = document.getElementById('more');
  if(div)
  {
    div.style.display = "none";
  }
}

function clearStub()
{
  var e = document.getElementById('custominput');
  if(e.value === '(custom value)')
  {
    e.value = '';
  }
  e.style.color = 'black';
}

function checkboxClicked(x)
{
  if(x.checked)
  {
    checkboxChecked++;
    if(x.id == "customcheck")
    {
      var e = document.getElementById('custominput');
      if(e.value !== '(custom value)' && e.value.length > 0)
      {
        whiteListTo[x.id] = e.value;
      }
    }
    else
    {
      whiteListTo[x.id] = x.id;
    }
  }
  else
  {
    checkboxChecked--;
    delete whiteListTo[x.id];
  }
  
  var button = document.getElementById('white');
  button.disabled = checkboxChecked  == 0; 
}

function writeCustomItem()
{
  var c = document.getElementById('customcheck')
  if(c.checked)
  {
    var e = document.getElementById('custominput');
    if(e.value !== '(custom value)' && e.value.length > 0)
    {
      whiteListTo['customcheck'] = e.value;
    }
  }
}

function reload()
{
  var c = document.getElementById('reload');
  if(c.checked)
  {
    chrome.tabs.reload(parseInt(c.getAttribute('tabid')), {bypassCache: true});
  }
}

function submitWhitelist()
{
  chrome.extension.getBackgroundPage().mergeWhiteListTo(values(whiteListTo));
  reload();
  window.close();
}

function submitOrigin(s)
{
  chrome.extension.getBackgroundPage().mergeWhiteListFrom([s]);
  reload();
  window.close();
}

function values(obj)
{
  var values = [];

  for(var key in obj)
  {
    if(obj.hasOwnProperty(key))
    {
      values.push(obj[key]);
    }
  }

  return values;
}

function keys(obj)
{
  var keys = [];

  for(var key in obj)
  {
    if(obj.hasOwnProperty(key))
    {
      keys.push(key);
    }
  }

  return keys;
}

function openTab(filename)
{
  var myid = chrome.i18n.getMessage("@@extension_id");
  chrome.windows.getCurrent(
  function(win)
  {
    chrome.tabs.query({'windowId': win.id},
    function(tabArray)
    {
      for(var i in tabArray)
      {
        if(tabArray[i].url == "chrome-extension://" + myid + "/" + filename)
        {
          console.log("already opened");
          chrome.tabs.update(tabArray[i].id, {active: true});
          return;
        }
      }
      chrome.tabs.create({url:chrome.extension.getURL(filename)});
    });
  });
}
 
function editWhiteLists()
{
  openTab("manage.html");
}

</script>
</head>

<body onLoad="showDummy()" style="width:360px;overflow-x:hidden;">

<img id="result" width="300" height="1"><img>

<div id="tableau"></div>

</body>
</html>
